package com.xxs.controller.front;

import java.util.Enumeration;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import com.jfinal.aop.Before;
import com.xxs.common.Const;
import com.xxs.common.controller.BaseController;
import com.xxs.interceptor.FrontInterceptor;
import com.xxs.utils.Context;
import com.xxs.utils.UUIDUtils;
import com.xxs.utils.xml.Fields;
import com.xxs.utils.xml.XmlDom4jUtils;

/**
 * Controller - Controller基类
 */
@Before(FrontInterceptor.class)
@SuppressWarnings("unchecked")
public class BaseFrontController extends BaseController {
	
	public void list(){
    	baseList();
    	render("list.html");
    }
	public void add(){
    	render("add.html");
    }
	public void edit(){
		baseEdit();
    	render("edit.html");
    }
    public void save(){
    	baseSave();
    	list();
    }
    public void update(){
    	baseUpdate();
    	list();
    }
	public void delete(){
    	baseDelete();
        list();
    }
	
	public String getCurrentSiteCode(){
		String siteCode = XmlDom4jUtils.getParaValueByAttrAndValue(Const.SITE_PATH,"site","id",Context.getCurrentSiteId(getSession()),"code");
		return siteCode;
	}
	public String getCurrentSiteName(){
		String siteName = XmlDom4jUtils.getParaValueByAttrAndValue(Const.SITE_PATH,"site","id",Context.getCurrentSiteId(getSession()),"name");
		return siteName;
	}
	
	public void baseList() {
		Element element = XmlDom4jUtils.getRootElement(getXmlPath(true));
    	List<Element> list = element.elements();
    	for (int i = 0; i < list.size(); i++) {
    		Element e = list.get(i);
    		System.out.println(e.attributeValue("name"));
    		List<Element> es = e.elements();
    		for (int j = 0; j < es.size(); j++) {
    			Element s = es.get(j);
    			System.out.println("---"+s.attributeValue("name"));
			}
		}
    	setAttr("fieldList", getFields());
    	setAttr("list", list);
	}
	public void baseEdit() {
		String id = getPara(0);
		String modelName = getAttrForStr("modelName");
    	Element element = null;
    	Document document = XmlDom4jUtils.getDocument(modelName);
    	List<Element> listElements = document.selectNodes("/"+modelName+"s/"+modelName+"[@id='"+id+"']");
    	if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}
    	setAttr("fieldList", getFields());
    	setAttr("id", id);
		setAttr("element", element);
	}
	public void baseDelete(){
		String id = getPara(0);
		String modelName = getAttrForStr("modelName");
    	Element element = null;
    	Document document = XmlDom4jUtils.getDocument(modelName);
    	List<Element> listElements = document.selectNodes("/"+modelName+"s/"+modelName+"[@id='"+id+"']");
    	if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    	}
    	document.getRootElement().remove(element);
    	try {
			XmlDom4jUtils.saveDocumentToFile(modelName, document);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
	public void baseSave(){
		String modelName = getAttrForStr("modelName");
		List<Fields> fieldList = getFields();
		Document document = XmlDom4jUtils.getDocument(modelName);
		Element element = document.getRootElement();
		Element newElement = element.addElement(modelName);
		newElement.setName(modelName);
		for (int i = 0; i < fieldList.size(); i++) {
			System.out.println("当前循环的属性名："+fieldList.get(i).getName()+"填充的内容"+getPara(fieldList.get(i).getName()));
			if("id".equals(fieldList.get(i).getName())){
				newElement.addAttribute(fieldList.get(i).getName(), UUIDUtils.creatUUID());
			}else{
				newElement.addAttribute(fieldList.get(i).getName(), getPara(fieldList.get(i).getName()));
			}
		}
		try {
			XmlDom4jUtils.saveDocumentToFile(modelName, document);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void baseUpdate(){
		String id = getPara(0);
		String modelName = getAttrForStr("modelName");
    	Document document = XmlDom4jUtils.getDocument(modelName);
    	List<Element> listElements = document.selectNodes("/"+modelName+"s/"+modelName+"[@id='"+id+"']");
    	Element element = null;
    	if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    		Enumeration<String> enumeration = getParaNames();
        	while(enumeration.hasMoreElements()){
        		String name=(String)enumeration.nextElement();
        		String value=getPara(name);
        		System.out.print(name + "=" + value);
        		Attribute attribute = element.attribute(name);  
        		if(attribute==null){
        			element.addAttribute(name, value);
        		}else{
        			attribute.setValue(value);
        		}
        	}
        	try {
        		XmlDom4jUtils.saveDocumentToFile(modelName, document);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
    	}
    }
}